package com.apestech.framework.xml.condition.node;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.Account;
import com.apestech.framework.xml.pub.core.ElementUtil;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2019-03-05-9:16
 */
public class NodeAccount extends ElementUtil {
    private Element node;
    private Account account;
    private SqlSession session;

    public NodeAccount(Element node,  SqlSession session){
        this.node=node;
        this.session=session;
        account=new Account(session);
    }

    public void getData() throws Exception {
        account.setAccount(getNodeAttr(node).getJSONObject("attrNode"));
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                Element ele=(Element) list.get(i);
                getField(ele);
            }
        }
    }

    private void getField(Element e) throws Exception {
        String theme=e.getName();
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject m=getNodeAttr(eml).getJSONObject("attrNode");
                String ename=eml.getName();
                switch (theme){
                    case "update":{
                        m.put("field", ename);
                        account.addUpdate(m);
                        break;
                    }
                    case "bill":{
                        m.put("id", ename);  //table_id
                        account.setBill(m);
                        getBill(ename, eml, m);
                        break;
                    }
                }
            }
        }
    }

    private void getBill(String id, Element e, JSONObject m) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject map=getNodeAttr(eml).getJSONObject("attrNode");
                map.put("update", eml.getName());
                map.putAll(m);
                account.addUpdateTable(id,map);
                getExecute(id, eml.getName(), eml);
            }
        }
    }

    private void getExecute(String id, String update, Element e) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject m=getNodeAttr(eml).getJSONObject("attrNode");
                m.put("id", eml.getName());
                account.addExecute(id, update, m);
            }
        }
    }

    public Account getAccount() {
        return account;
    }
}
